import random

from .base_layer import BaseLayer
from . import utils

FULL_RANDOM = False  # 通过此变量来确定信息战是完全随机，还是有一点策略性


class EncMsgCrackLayer(BaseLayer):
    def __init__(self):
        self.color = None

    def setup(self, color):
        self.color = color

    def step(self):
        action_list = None
        if FULL_RANDOM:
            action_list = self.random_strategy()
        else:
            action_list = self.with_brain_strategy()
        return action_list

    def random_strategy(self):
        """
        完全随机的作战风格
        """
        has_choose = {
            301: False,
            302: False,
            303: False,
            304: False
        }
        action_list = []
        current_attack_num = utils.get_current_attack_num()
        # 循环执行0～4次
        for _ in range(random.randint(0, 4)):
            # 随机选择一个动作进行尝试生成
            random_number = random.randint(1, 4)
            if random_number == 1:
                if has_choose[301] == True:
                    continue
                # 1、尝试进行301-暴力密钥破解
                action = self.gen_key_crack_force()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[301] = True
            elif random_number == 2:
                # 2、尝试进行302-后门密钥破解
                if has_choose[302] == True:
                    continue
                action = self.gen_key_crack_advance()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[302] = True
            elif random_number == 3:
                # 3、尝试进行303-瘫痪攻击
                if has_choose[303] == True:
                    continue
                action = self.gen_cripple_attack()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[303] = True
            elif random_number == 4:
                # 4、尝试进行304-无线信号干扰
                if has_choose[304] == True:
                    continue
                action = self.gen_wireless_signal_interference()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[304] = True
        utils.set_current_attack_num(current_attack_num)
        return action_list

    def with_brain_strategy(self):
        """
        带点脑子的作战风格
        """
        has_choose = {
            301: False,
            302: False,
            303: False,
            304: False
        }
        action_list = []
        current_attack_num = utils.get_current_attack_num()
        # 循环执行0～4次
        for _ in range(random.randint(0, 4)):
            # 随机选择一个动作进行尝试生成
            random_number = random.randint(1, 4)
            if random_number == 1:
                # 1、尝试进行301-暴力密钥破解
                if has_choose[301] == True:
                    continue
                action = self.gen_key_crack_force()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[301] = True
            elif random_number == 2:
                # 2、尝试进行302-后门密钥破解
                if has_choose[302] == True:
                    continue
                action = self.gen_key_crack_advance()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[302] = True
            elif random_number == 3:
                # 3、尝试进行303-瘫痪攻击
                if has_choose[303] == True:
                    continue
                action = self.gen_cripple_attack()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[303] = True
            elif random_number == 4:
                pass
        #  无线通信干扰：上一时刻通信质量高于阈值（0.7）
        if utils.get_communication_quality()["blue" if self.color == 0 else "red"] >= 0.7:
            action = self.gen_wireless_signal_interference()
            if action and utils.action_valid(action):
                current_attack_num["red" if self.color == 0 else "blue"] += 1
                action_list.append(action)
        utils.set_current_attack_num(current_attack_num)
        return action_list

    def gen_key_crack_force(self):
        # 1、获取已成功伪装接入且未死亡的对方算子的id构成的列表
        has_disguised_access_ids = utils.get_peer_alive_has_disguised_access_ids(self.color)
        # 2、随机取出一个活着的算子id作为攻击对象，如果算子都死亡了则直接返回
        if len(has_disguised_access_ids) == 0:
            return None
        ops_id = random.choice(has_disguised_access_ids)
        # 3、生成动作
        action = {
            "color": self.color,
            "type": 301,
            "ops_id": ops_id,
            "remain_time": utils.ACTION_COST_TIME[301]
        }
        return action

    def gen_key_crack_advance(self):
        # 1、获取已成功伪装接入且未死亡的对方算子的id构成的列表
        has_disguised_access_ids = utils.get_peer_alive_has_disguised_access_ids(self.color)
        # 2、随机取出一个算子id作为攻击对象
        if len(has_disguised_access_ids) == 0:
            return None
        ops_id = random.choice(has_disguised_access_ids)
        # 3、生成动作
        action = {
            "color": self.color,
            "type": 302,
            "ops_id": ops_id,
            "remain_time": utils.ACTION_COST_TIME[302]
        }
        return action

    def gen_cripple_attack(self):
        # 1、获取对方还未死亡算子的id构成的列表
        alive_ids = utils.get_peer_alive_ids(self.color)
        # 2、随机取出一个算子id作为攻击对象
        if len(alive_ids) == 0:
            return None
        ops_id = random.choice(alive_ids)
        # 3、生成动作
        action = {
            "color": self.color,
            "type": 303,
            "ops_id": ops_id,
            "remain_time": utils.ACTION_COST_TIME[303]
        }
        return action

    def gen_wireless_signal_interference(self):
        """
        生成无线信号干扰动作
        """
        # 生成动作
        action = {
            "color": self.color,
            "type": 304,
            "remain_time": utils.ACTION_COST_TIME[304]
        }
        return action

    def reset(self):
        pass
